﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dispatch.Utils
{
    /// <summary>
    /// 公共函数
    /// </summary>
    public class Tools
    {
        /// <summary>
        ///对象拷贝
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T?  Copy<T>(T obj) where T : class
        {

            Type targetType = obj.GetType();
            var   targetDeepCopyObj = System.Activator.CreateInstance(targetType)  ;   //创建引用对象 
            if (targetDeepCopyObj == null)
            {
                return null;
            }
              
            MemberInfo[] memberCollection = obj.GetType().GetMembers(); 
            foreach ( MemberInfo member in memberCollection)
            {
                if (member.MemberType == MemberTypes.Field)
                {
                    FieldInfo field = ( FieldInfo)member;
                    Object? fieldValue = field.GetValue(obj);
                     field.SetValue(targetDeepCopyObj, fieldValue ); 
                   

                }
                else if (member.MemberType ==  MemberTypes.Property)
                {
                    PropertyInfo myProperty = ( PropertyInfo)member;
                    MethodInfo? info = myProperty.GetSetMethod(false);
                    if (info != null)
                    {
                        object? propertyValue = myProperty.GetValue(obj, null);
                        myProperty.SetValue(targetDeepCopyObj, propertyValue, null); 
                    }
                } 
            }
            return (T)targetDeepCopyObj;
        }

        /// <summary>
        /// 向目标传输坞发送消息 
        /// </summary>
        /// <param name="LocalPort"></param>
        /// <param name="DeviceIP"></param>
        /// <param name="DeivcePort"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Task SendDataToDeviceAsync(int LocalPort, string DeviceIP, int DeivcePort, byte[] data)
        {
            try
            {
                IPEndPoint local = new IPEndPoint(IPAddress.Parse("127.0.0.1"), LocalPort); //本地发送，注意多网卡模式
                UdpClient udpClient = new UdpClient(local);
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(DeviceIP), DeivcePort); //目标 
                udpClient.Send(data, data.Length, iPEndPoint);
                udpClient.Close();
            }
            catch (Exception ex)
            {

            } 
            return Task.CompletedTask;
        }


        /// <summary>
        /// 向AGV发送消息
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="Port"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Task SendDataToAgvAsync(string  IP, int  Port, byte[] data)
        {
            try
            { 
                UdpClient udpClient = new UdpClient(5000); //约定的发送端口
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port); //目标 
                udpClient.Send(data, data.Length, iPEndPoint);
                udpClient.Close();
            }
            catch (Exception ex)
            {

            }
            return Task.CompletedTask;
        }

    }
}
